package com.c39.otc.open.api.customer;

import com.c39.center.custom.facade.EmpCustomerFacade;
import com.c39.center.custom.model.customer.dto.EmpCustomDTO;
import com.c39.center.custom.model.customer.vo.CustomerCenterListVO;
import com.c39.otc.open.api.customer.request.CustomerRequest;
import com.c39.otc.open.api.customer.response.CustomerResponse;
import com.cqfan.cloud.framework.base.PagedResult;
import com.cqfan.cloud.framework.base.Result;
import com.cqfan.cloud.framework.base.ResultCodeEnum;
import com.cqfan.cloud.framework.global.LogicI18nException;
import com.cqfan.cloud.framework.utils.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.security.auth.login.LoginException;
import java.util.List;

/**
 * 客户模块
 * @author JiaLing.FanX
 * @version 1.0
 * @description: TODO
 * @date 2025/9/10 16:21
 */

@RestController
@RequestMapping("/v1/customer")
public class CustomerController {

    @Resource
    private EmpCustomerFacade empCustomerFacade;


    /**
     * 获取员工所属客户列表
     * @param customerRequest
     * @mock 1023934
     * @return
     * @throws Exception
     */
    @PostMapping("/getEmpCustomersByEmpNum")
    public PagedResult<List<CustomerResponse>> getEmpCustomersByEmpNum(@RequestBody CustomerRequest customerRequest) throws Exception {
        EmpCustomDTO empCustomDTO = new EmpCustomDTO();
        empCustomDTO.setEmpNumber(customerRequest.getEmpNum());
        empCustomDTO.setState(0);
        PagedResult<List<CustomerCenterListVO>> result = empCustomerFacade.customListBusiness(empCustomDTO);
        if(result.isSuccess() == false && !result.code.equals("0")){
            PagedResult pagedResult = new PagedResult();
            pagedResult.setSuccess(false);
            pagedResult.setCode("-1");
            pagedResult.setMessage(result.getMessage());
            return pagedResult;
        }

        List<CustomerCenterListVO> data = (List<CustomerCenterListVO>) result.getData();
        List<CustomerResponse> customerResponses = BeanUtils.copyPropertiesList(data , CustomerResponse.class);
        PagedResult pagedResult = new PagedResult();
        pagedResult.setSuccess(true);
        pagedResult.setData(customerResponses);
        pagedResult.setCode(ResultCodeEnum.SUCCESS.getCode());
        pagedResult.setMessage(ResultCodeEnum.SUCCESS.getMsg());
        pagedResult.setTotal(result.getTotal());
        pagedResult.setPages(result.getPages());
        pagedResult.setPageNum(result.getPageNum());
        pagedResult.setPageSize(result.getPageSize());
        return pagedResult;
    }

}
